package org.mzy.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.mzy.basic.anno.PreAuthorize;
import org.mzy.basic.util.AjaxResult;
import org.mzy.basic.util.PageList;
import org.mzy.org.domain.SystemDictionaryType;
import org.mzy.org.query.SystemDictionaryTypeQuery;
import org.mzy.org.service.ISystemDictionaryDetailService;
import org.mzy.org.service.ISystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Package: org.mzy.org.controller
 * @ClassName: SystemdictionarytypeController
 * @Date:2022/10/13 10:09
 * @Description:
 */

@RestController
@RequestMapping("/systemDictionaryType")
@Api(value = "数据字典明细的API", description = "相关的CRUD功能")
public class SystemDictionaryTypeController {
    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    // 查询全部
    @GetMapping
    @ApiOperation(value = "查询所有信息")
    @PreAuthorize(name = "查询数据字典类型", sn = "systemDictionaryType:findAll")
    public List<SystemDictionaryType> findAll() {
        return systemDictionaryTypeService.findAll();
    }

    // 根据ID删除单条
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除一条数据")
    @PreAuthorize(name = "删除数据字典类型", sn = "systemDictionaryType:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            systemDictionaryTypeService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }

    // 批量删除
    @PatchMapping
    @ApiOperation("批量删除")
    @PreAuthorize(name = "批量删除数据字典类型", sn = "systemDictionaryType:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        try {
            systemDictionaryTypeService.batchDel(ids);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }

    // 新增或修改
    @PutMapping
    @ApiOperation(value = "新增或修改")
    @PreAuthorize(name = "新增/修改数据字典类型", sn = "systemDictionaryType:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType systemdictionarytype) {
        if (systemdictionarytype.getId() == null) {
            systemDictionaryTypeService.add(systemdictionarytype);
        } else {
            systemDictionaryTypeService.update(systemdictionarytype);
        }
        return AjaxResult.me().success();
    }

    // 根据ID查询单条
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询一条数据")
    @PreAuthorize(name = "根据id查询数据字典类型", sn = "systemDictionaryType:findById")
    public SystemDictionaryType findById(@ApiParam(value = "ID属性", required = true) @PathVariable("id") Long id) {
        return systemDictionaryTypeService.findById(id);
    }

    // 分页查询
    @PostMapping
    @ApiOperation("分页查询")
    @PreAuthorize(name = "分页查询数据字典类型", sn = "systemDictionaryType:queryByPage")
    public PageList<SystemDictionaryType> queryByPage(@RequestBody SystemDictionaryTypeQuery systemdictionarytypeQuery) {
        return systemDictionaryTypeService.queryByPage(systemdictionarytypeQuery);
    }
}
